﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using Microsoft.SharePoint;
using DAL = MS.InformationManagement.DAL.Support;
using Microsoft.SharePoint.Taxonomy;

namespace MS.InformationManagement.Presenter.RequestVoBo
{
    public class RequestVoBoPresenter : BasePresenter<IRequestVoBoView>
    {
        #region Constructors

        public RequestVoBoPresenter(IRequestVoBoView view) : base(view) { }

        #endregion

        #region public Methods

        public void RequestVoBo()
        {
            new BLL.RequestBoVo.RequestVoBo().RequestForVoBo(View.SelectedIds, View.SourceList,View.TargetUser, View.RequestMessage);
            View.Success();
        }

        public void HandleLoad()
        {
            View.DepartmentUsers = new BLL.RequestBoVo.RequestVoBo().GetUsers();
                //PeopleHelper.GetUsersByProperty(GlobalConstants.UP_DEPARTMENT, PeopleHelper.GetPropertyCurrentUserProfile(GlobalConstants.UP_DEPARTMENT));
            View.SelectedFilesInfo = getSelectedInfoFiles();                                    
            View.Show();
        }

        #endregion

        #region Private Methods

        private Dictionary<string, string> getSelectedInfoFiles()
        {
            Dictionary<string, string> files = new Dictionary<string, string>();
            for (int i = 0; i < View.SelectedIds.Length - 1; i++ )
            {
                int id = View.SelectedIds[i];
                SPFile currentFile = DAL.Support.CamlHelper.GetDocumentById(View.SourceList, id, SPContext.Current.Web);
                if (currentFile == null) throw new Exception("Alguno de los documentos seleccionados no son archivos validos para la operación");
                string note = string.Empty;
                if (currentFile.Item[CustomSiteColumns.FIELD_DOCUMENT_CATEGORY] != null && ((TaxonomyFieldValue)currentFile.Item[CustomSiteColumns.FIELD_DOCUMENT_CATEGORY]).Label == GlobalConstants.METADATA_TERM_CATEGORY_VOBO)
                    note = ExceptionConstants.ERROR_STATUS;
                files.Add(currentFile.Name, note);
            }
            return files;
        }

        #endregion
    }
}
